package homwwork02;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.util.List;

public class dom4jTest {

    public static void main(String[] args)  {

        //1.获取XML解析对象
        SAXReader reader = new SAXReader();
        //2.解析XML 获取 文档对象 document
        try {
            Document document = reader.read("D:\\lg\\jdbc_homework\\src\\homwwork02\\books.xml");
            for (int i = 1; i <=3; i++) {
                Node node1 = document.selectSingleNode("/books/book["+i+"]/attribute::出版社");
                Node node2 = document.selectSingleNode("/books/book["+i+"]/name");
                Node node3 = document.selectSingleNode("/books/book["+i+"]/author");
                Node node4 = document.selectSingleNode("/books/book["+i+"]/price");
                Node node5 = document.selectSingleNode("/books/book["+i+"]/body");
                System.out.println("第"+i+"本书：出版社为："+node1.getText() +" 书名为："+ node2.getText() +" 作者是："+ node3.getText() +" 价格为："+ node4.getText() +" "+ node5.getText());
            }





        } catch (DocumentException e) {
            e.printStackTrace();
        }


    }



}
